package com.example.easeimpro.controller.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;

import com.example.easeimpro.R;
import com.example.easeimpro.controller.adapter.GroupListAdapter;
import com.example.easeimpro.model.bean.Model;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.easeui.widget.EaseTitleBar;
import com.hyphenate.exceptions.HyphenateException;

import java.util.List;

public class GroupActivity extends BaseActivity {

    private RecyclerView rl_groupList;
    private GroupListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group);
        initView();
        ((EaseTitleBar)findViewById(R.id.friend_titlebar))
                .setTitle("群组列表");
        ((EaseTitleBar)findViewById(R.id.friend_titlebar))
                .setOnBackPressListener(this);
        initData();
    }

    private void initData() {
        adapter = new GroupListAdapter(mContext);
        rl_groupList.setAdapter(adapter);
        rl_groupList.setLayoutManager(new LinearLayoutManager(mContext));
        getGroupFromService();
    }

    private void getGroupFromService() {
        Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    List<EMGroup> emGroups = EMClient.getInstance().groupManager().getJoinedGroupsFromServer();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter.refresh(EMClient.getInstance().groupManager().getAllGroups());
                        }
                    });
                    //刷新页面

                } catch (HyphenateException e) {
                    e.printStackTrace();
                    showToast("群加载失败");
                }

            }
        });
    }



    @Override
    protected void onResume() {
        super.onResume();
        adapter.refresh(EMClient.getInstance().groupManager().getAllGroups());
    }

    private void initView() {
        rl_groupList = findViewById(R.id.rl_grouplist);


    }


}